/*
 *	append
 *	Appends one thing to another
 *
 */
objx.fn.append = function() {

	if (arguments.length === 1) {

		var 
			app = arguments[0],
			appType = objx.type(app),
			to = this.obj(),
			toType = this.type(),
			error = false
		;
		
		if (appType == "array") {
		
			if (objx.debug) {
				// we require each
				objx.requires("objx.each", "objx.append (" + appType + " to " + toType + ")");
			}
			
			// add each item in the array to the current array
			objx(app).each(objx.bind(function(item){
				this.append(item);
			}, this));
		
		} else {
			
			switch (toType) {
				case "string":
				
					switch (appType) {
						case "string":
						case "number":
						case "boolean":
							
							this.obj(to += app.toString());
							
							break;
						case "function":
							error = true;
							break;
					}
				
					break;
				case "number":
	
					switch (appType) {
					
						case "boolean":
							error = true;
							break;
						case "function":
							error = true;
							break;
						default:
							
							this.obj(to + parseFloat(app));
							
							break;
					}
	
					break;
				case "boolean":
				
					switch (appType) {
						case "number":
						case "function":
							error = true;
							break;
						default:
						
							if (objx.debug) {
								// we require each
								objx.requires("objx.type", "objx.append (" + appType + " to " + toType + ")");
							}
							
							this.obj(to && objx(app).type("boolean").obj());
							
							break;
					}
				
					break;
				case "array":
				
					to.push(app);

					break;
				
				case "function":
				
					if (appType == "function") {
					
						var originalFunction1 = this.obj();
	
	                    this.obj(
	                        (function(){
	                          
	                          var __orig = originalFunction1;
	                          var __new = app;
	                          
	                          return function(){
	                            __orig.apply(this, arguments);
	                            __new.apply(this, arguments);
	                          };
	    
	                        })()
	                    );
					
					} else {

     					var originalFunction2 = this.obj();
	                    
	                    this.obj(
	                             (function(){
	                               
	                               var __orig = originalFunction2;
	                               var __new = app;
	                               
	                               return function() {
	                                 return objx(__orig.apply(this, arguments)).append(__new).obj();
	                               };
	
	                               })()
	                    );
					
					}
					
					break;
			}
			
		}

	} else {
	
		// handle each argument
		objx(arguments).each(

			// use bind to assign the first parameter
			objx.bind(
			
				// function to call .is() for each argument
				function(context, o) {
				
					// capture the 'passed' value
					context.append(o);
					
				}, 
				
				// we dont care about binding context, just arguments
				null, 
				
				// the first argument should be this context
				this)
				
		);
	
	}
	
	if (error === true) {
		objx.error("objx.append doesn't know how to append " + appType + "'s to " + toType + "'s.");
	}
	
	// return this for chaining
	return this;

};

objx.provides("objx.append");